package com.example.onlinemarket.model;

import android.os.Parcel;
import android.os.Parcelable;

import static com.example.onlinemarket.Application.download;
import static com.example.onlinemarket.util.FileUtil.externalFilesDir;

public class User implements Parcelable {
    private Integer userId;//用户ID
    private String userName;//用户名
    private String password;//密码
    private String email;//邮箱
    private boolean role;//角色（商家/买家）
    private String imgPath;//服务器上头像文件获取地址
    private String localPath;//本地头像文件存放位置
    private String address;//收货地址
    private String payPassword;//支付密码

    protected User(Parcel in) {
        if (in.readByte() == 0) {
            userId = null;
        } else {
            userId = in.readInt();
        }
        userName = in.readString();
        password = in.readString();
        email = in.readString();
        role = in.readByte() != 0;
        imgPath = in.readString();
        localPath = in.readString();
        address = in.readString();
        payPassword = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getPayPassword() {
        return payPassword;
    }

    public void setPayPassword(String payPassword) {
        this.payPassword = payPassword;
    }

    //从服务器获取用户头像
    public void getUserImg(){
        download(imgPath);
        localPath = externalFilesDir+imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isRole() {
        return role;
    }

    public void setRole(boolean role) {
        this.role = role;
    }

    public String getImgPath() {
        return imgPath;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (userId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(userId);
        }
        dest.writeString(userName);
        dest.writeString(password);
        dest.writeString(email);
        dest.writeByte((byte) (role ? 1 : 0));
        dest.writeString(imgPath);
        dest.writeString(localPath);
        dest.writeString(address);
        dest.writeString(payPassword);
    }
}
